package seq_diverse.main;

import java.awt.Color;
import java.awt.BasicStroke;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYAreaRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class LineChart extends ApplicationFrame {
    
    public LineChart(final String title, final XYDataset data1, final XYDataset data2) {
	super(title);

	final JFreeChart chart = createChart2(data1, data2);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
	
    }

    private JFreeChart createChart2(final XYDataset data1, final XYDataset data2){
	//plot and LineChart
	final NumberAxis xAxis = new NumberAxis("k-mer Position");
        xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	final NumberAxis yAxis = new NumberAxis("Diversity Metrics");
        yAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
	final XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
	
	final XYPlot plot = new XYPlot(data1, xAxis, yAxis, renderer1);
		
	//AreaChart
	final XYAreaRenderer renderer2 = new XYAreaRenderer();
	renderer2.setSeriesVisibleInLegend(1, false);
	plot.setDataset(1, data2);
	plot.setRenderer(1, renderer2);
	plot.setForegroundAlpha(0.5f);
	
	//plot.setBackgroundPaint(Color.lightGray);                                                                                                                    
        plot.setDomainGridlinePaint(Color.black);
        plot.setRangeGridlinePaint(Color.black);

	return new JFreeChart("Diversity Measure of Sequence k-mers", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    }

    private JFreeChart createChart(final XYDataset dataset) {

                                                                                                                                                                                 
        final JFreeChart chart = ChartFactory.createXYLineChart(
                                                                "Diversity Measure of Sequence K-mers",                                                                           
                                                                "k-mer Position",                                                                                                         
                                                                "Diversity Metrics",
								dataset,
								PlotOrientation.VERTICAL,
                                                                true,                                                                                                                 
                                                                true,                                                                                                                    
                                                                false
								);

                                                                                                                                                  
        chart.setBackgroundPaint(Color.white);
                                                                                                                                             

                                                                                                                                    
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);                                                                                                                    
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
	renderer.setSeriesShapesVisible(0, false);

        renderer.setSeriesStroke(
                                 0,
                                 new BasicStroke(
                                                 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                                                 1.0f, new float[] {6.0f, 6.0f}, 0.0f
                                                 ));

	renderer.setSeriesPaint(3, Color.RED);

        plot.setRenderer(renderer);

                                                                                                                                       
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
                                                                                                                                                                  

        return chart;

    }
}

